package com.quality.web.controller.business;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.quality.business.domain.QualityRule;
import com.quality.business.service.QualityRuleService;
import com.quality.common.annotation.Log;
import com.quality.common.core.controller.BaseController;
import com.quality.common.core.domain.AjaxResult;
import com.quality.common.core.page.TableDataInfo;
import com.quality.business.domain.QualityGroup;
import com.quality.business.service.QualityGroupService;
import com.quality.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author hjh
 * @Description TODO
 * @createTime 2022年06月25日 17:47:00
 */
@PreAuthorize("@ss.hasAnyRoles('common,common_admin,admin')")
@RestController
@RequestMapping("/quality/group")
public class QualityGroupController extends BaseController {

    @Autowired
    private QualityGroupService groupService;
    @Autowired
    private QualityRuleService ruleService;

    @GetMapping("/list")
    public TableDataInfo list(String groupName){
        LambdaQueryChainWrapper<QualityGroup> w = groupService.lambdaQuery();
        if (StrUtil.isNotEmpty(groupName)){
            w.like(QualityGroup::getGroupName,groupName);
        }
        if (!getUserId().equals(1L)){
            w.eq(QualityGroup::getUserId,getUserId());
        }
        startPage();
        List<QualityGroup> list = w.list();
        return getDataTable(list);
    }

    @GetMapping("/all")
    public AjaxResult all(){
        LambdaQueryChainWrapper<QualityGroup> w = groupService.lambdaQuery();
        if (!getUserId().equals(1L)){
            w.eq(QualityGroup::getUserId,getUserId());
        }
        return AjaxResult.success(w.list());
    }

    @GetMapping("/byId")
    public AjaxResult byId(Integer id){
        return AjaxResult.success(groupService.getById(id));
    }

    @Log(title = "质量管理分组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody QualityGroup group){
        errIf(StrUtil.isEmpty(group.getGroupName()),"分组名称不能为空");
        group.setUserId(getUserId());
        groupService.save(group);
        return AjaxResult.success();
    }

    @Log(title = "质量管理分组", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody QualityGroup group){
        errIf(StrUtil.isEmpty(group.getGroupName()),"分组名称不能为空");
        groupService.updateById(group);
        return AjaxResult.success();
    }

    @Log(title = "质量管理分组", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable("ids") Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        errIf(CollectionUtil.isEmpty(list),"请选择数据");
        groupService.removeByIds(list);
        ruleService.remove(ruleService.lambdaQuery().in(QualityRule::getGroupId,list).getWrapper());
        return AjaxResult.success();
    }
}
